
import time

import requests
import re
import os
import shutil
from util.logger import get_looger

logger = get_looger("高清壁纸.log", __file__)

logger.info("爬虫启动......")

try:
    root = 'static/高清壁纸/'
    if os.path.exists(root):
        shutil.rmtree(root)
    os.makedirs(root)

    url = 'https://www.hdbz.net'

    res = requests.get(url)

    html_content = res.content.decode('utf8')
    items = re.findall(
        r'<a href="(/wallpaper/\d{6}/\d{6}.html)" target="_blank" title="(.*?)".*?class="item">.*?<img.*?data-original="(.*?)".*?class="lazy" />',
        html_content, re.S)
    for item in items:
        time.sleep(1)
        title = item[1]
        logger.info(f"开始爬取{title}")

        thumb_url = item[2]
        logger.info(f"开始爬取{title} 缩略图")
        thumb_response = requests.get(thumb_url)
        with open(f"{root}{title}_thumb.png", "wb") as f:
            f.write(thumb_response.content)
        logger.info(f"保存{title}缩略图成功")

        preview_url = f"{url}{item[0]}"
        preview_response = requests.get(preview_url).content.decode('utf8')
        r = re.search(r'<div class="pic-preview">.*?<img alt=.*?src="(.*?)"', preview_response, re.S)
        if r:
            preview_image_url = r.group(1)
            logger.info(f"开始爬取{title} 预览图")
            preview_image_response = requests.get(preview_image_url)
            with open(f"{root}{title}_preview.png", "wb") as f:
                f.write(preview_image_response.content)
            logger.info(f"保存{title}预览图成功")
except Exception as e:
    logger.error(e)

logger.info("爬虫结束......")
